﻿using UnityEngine;
using System.Collections;

namespace DragonRescue3D
{
    /// <summary>
    /// Defines the behaviour for a dragon object in the scene
    /// </summary>
    public class Dragon : GridObject
    {
        public GameObject _savedEffectPrefab;
        public GameObject _scoreAdditionPrefab;

        //--------------------------------------------------------
        /// <summary>
        /// Checks to see if we have hit the ground.  If so we have been saved!
        /// </summary>
        /// <param name="col"></param>
        void OnCollisionEnter(Collision col)
        {
            // Have we hit the floor?
            if (col.transform.tag == "Floor")
            {
                IncrementScoreAndRemove();
            }
        }

        //--------------------------------------------------------
        /// <summary>
        /// Instantiates saved effect prefab, score addition prefab and removes us from the scene.
        /// </summary>
        void IncrementScoreAndRemove()
        {
            Instantiate(_savedEffectPrefab, transform.position, transform.rotation);
            Instantiate(_scoreAdditionPrefab, transform.position, transform.rotation);
            Destroy(gameObject);
        }

        //--------------------------------------------------------
        /// <summary>
        /// Determines if the dragon is stranded on a block.  If the floor is directly below us we
        /// must be falling with no blocks in the way and so we're not stranded.
        /// </summary>
        public bool IsStranded
        {
            get
            {
                bool stranded = false;
                Ray downRay = new Ray(transform.position, Vector3.down);
                RaycastHit[] downHits = Physics.RaycastAll(downRay);

                // Test for blocks below us
                foreach (RaycastHit hit in downHits)
                {
                    // Have we hit a block?
                    if (hit.transform.GetComponent<GridObject>() != null)
                    {
                        stranded = true;
                        break;
                    }
                }
                return stranded;
            }
        }
    }
}

#if NO_AND_ERROR
public void TEXT1() 
{
	int textNum = 1;
	if (textNum == 1) 
	{
		textNum = 2;
    }
}
public void TEXT2()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
public void TEXT3()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT4()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT5()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT6()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT8()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT9()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT10()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT11()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT12()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT13()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT14()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT15()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT16()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT17()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
#endif
